using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Acme.BookStore.StockManagement.Dtos.LiuShuiGuanLianPiCi;
using Acme.BookStore.StockManagement.Services;
using AutoMapper;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Acme.BookStore.Repositories;

namespace Acme.BookStore.StockManagement
{
    /// <summary>
    /// 出入库流水关联批次详情应用服务实现类
    /// </summary>
    public class LiuShuiGuanLianPiCiAppService : ApplicationService, ILiuShuiGuanLianPiCiAppService
    {
        private readonly IRepository<LiuShuiGuanLianPiCi, int> _liuShuiGuanLianPiCiRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="liuShuiGuanLianPiCiRepository">出入库流水关联批次详情仓储</param>
        /// <param name="mapper">对象映射</param>
        public LiuShuiGuanLianPiCiAppService(
            IRepository<LiuShuiGuanLianPiCi, int> liuShuiGuanLianPiCiRepository,
            IMapper mapper)
        {
            _liuShuiGuanLianPiCiRepository = liuShuiGuanLianPiCiRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取出入库流水关联批次详情分页列表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns>分页结果</returns>
        public async Task<PagedResultDto<LiuShuiGuanLianPiCiDto>> GetListAsync(LiuShuiGuanLianPiCiGetListInputDto input)
        {
            // 构建查询条件
            var query = _liuShuiGuanLianPiCiRepository.GetQueryable();

            // 添加查询条件
            if (!string.IsNullOrEmpty(input.IoOrderCode))
            {
                query = query.Where(x => x.IoOrderCode.Contains(input.IoOrderCode));
            }

            if (input.IoDate.HasValue)
            {
                query = query.Where(x => x.IoDate.Date == input.IoDate.Value.Date);
            }

            if (!string.IsNullOrEmpty(input.BusinessType))
            {
                query = query.Where(x => x.BusinessType == input.BusinessType);
            }

            if (input.DocumentTime.HasValue)
            {
                query = query.Where(x => x.DocumentTime.Date == input.DocumentTime.Value.Date);
            }

            if (!string.IsNullOrEmpty(input.WarehouseName))
            {
                query = query.Where(x => x.WarehouseName == input.WarehouseName);
            }

            if (input.OutQuantityMin.HasValue)
            {
                query = query.Where(x => x.OutQuantity >= input.OutQuantityMin.Value);
            }

            if (input.OutQuantityMax.HasValue)
            {
                query = query.Where(x => x.OutQuantity <= input.OutQuantityMax.Value);
            }

            if (input.InQuantityMin.HasValue)
            {
                query = query.Where(x => x.InQuantity >= input.InQuantityMin.Value);
            }

            if (input.InQuantityMax.HasValue)
            {
                query = query.Where(x => x.InQuantity <= input.InQuantityMax.Value);
            }

            // 获取总数
            var totalCount = await AsyncExecuter.CountAsync(query);

            // 排序
            if (!string.IsNullOrEmpty(input.Sorting))
            {
                query = query.OrderBy(x => x.Id);
            }
            else
            {
                query = query.OrderByDescending(x => x.Id);
            }

            // 分页
            query = query.Skip(input.SkipCount).Take(input.MaxResultCount);

            // 获取数据
            var items = await AsyncExecuter.ToListAsync(query);

            // 转换为DTO
            var dtos = _mapper.Map<List<LiuShuiGuanLianPiCi>, List<LiuShuiGuanLianPiCiDto>>(items);

            // 返回分页结果
            return new PagedResultDto<LiuShuiGuanLianPiCiDto>(totalCount, dtos);
        }

        /// <summary>
        /// 获取出入库流水关联批次详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>出入库流水关联批次详情</returns>
        public async Task<LiuShuiGuanLianPiCiDto> GetAsync(int id)
        {
            var entity = await _liuShuiGuanLianPiCiRepository.GetAsync(id);
            return _mapper.Map<LiuShuiGuanLianPiCi, LiuShuiGuanLianPiCiDto>(entity);
        }

        /// <summary>
        /// 创建出入库流水关联批次详情
        /// </summary>
        /// <param name="input">出入库流水关联批次详情</param>
        /// <returns>创建后的出入库流水关联批次详情</returns>
        public async Task<LiuShuiGuanLianPiCiDto> CreateAsync(LiuShuiGuanLianPiCiDto input)
        {
            var entity = _mapper.Map<LiuShuiGuanLianPiCiDto, LiuShuiGuanLianPiCi>(input);
            entity = await _liuShuiGuanLianPiCiRepository.InsertAsync(entity);
            return _mapper.Map<LiuShuiGuanLianPiCi, LiuShuiGuanLianPiCiDto>(entity);
        }

        /// <summary>
        /// 更新出入库流水关联批次详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="input">出入库流水关联批次详情</param>
        /// <returns>更新后的出入库流水关联批次详情</returns>
        public async Task<LiuShuiGuanLianPiCiDto> UpdateAsync(int id, LiuShuiGuanLianPiCiDto input)
        {
            var entity = await _liuShuiGuanLianPiCiRepository.GetAsync(id);
            _mapper.Map(input, entity);
            entity = await _liuShuiGuanLianPiCiRepository.UpdateAsync(entity);
            return _mapper.Map<LiuShuiGuanLianPiCi, LiuShuiGuanLianPiCiDto>(entity);
        }

        /// <summary>
        /// 删除出入库流水关联批次详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _liuShuiGuanLianPiCiRepository.DeleteAsync(id);
        }
    }
} 